<script setup lang="ts" name="GoodsSku">
import { GoodsInfo, Spec, SpecValue } from '@/types/goods'
import bwPowerSet from '@/utils/power-set'
const props = defineProps<{
  goods: GoodsInfo
  skuId?: String
}>()

const emit = defineEmits<{
  (e: 'changeSku', skuId: string): void
}>()

//初始化时，设置默认选中效果
const initSelectedSpec = () => {
  const sku = props.goods.skus.find((item) => item.id === props.skuId)
  console.log(sku)
  if (sku) {
    props.goods.specs.forEach((item, index) => {
      const value = sku.specs[index].valueName
      console.log(value)
      //让value对应的规格选中
      const spec = item.values.find((item) => item.name === value)
      spec!.selected = true
    })
  }
}
initSelectedSpec()

// 点击选中规格
// spec: 规格
// val: 规格的值
const changeSelected = (spec: Spec, sub: SpecValue) => {
  if (sub.disabled) return
  // 增加一个selected
  if (sub.selected) {
    sub.selected = false
  } else {
    // 让所有的都不选中
    spec.values.forEach((item) => {
      item.selected = false
    })
    // 让自己选中
    sub.selected = true
  }
  // 获取选中的值
  // getSelectedSpec(props.goods.specs)
  // 在点击规格的时候，需要更新禁用状态
  // 更新禁用状态
  updateDisabledStatus()

  // 1. 判断规格是否全部选中
  const selected = getSelectedSpec().filter((v) => v)
  if (selected.length === props.goods.specs.length) {
    // 说明全部选中
    // 2. 去pathMap找到对应的skuid
    const key = selected.join('★')
    const [skuId] = pathMap[key]
    // 3. 子传父，给父组件
    emit('changeSku', skuId)
  }
}

//   获取路径字典
const getPathMap = () => {
  const pathMap: any = {}
  // 1. 过滤掉库存为0的sku
  const skus = props.goods.skus.filter((item) => item.inventory > 0)
  // console.log(skus)
  // 2. 遍历有效的sku,获取sku的幂集
  skus.forEach((item) => {
    const arr = item.specs.map((sub) => sub.valueName)
    // console.log(arr)
    // 3. 调用powerSet获取幂集
    const powerSet = bwPowerSet(arr)
    // 4. 把这些powerSet合并到一个路径字典中
    powerSet.forEach((sub) => {
      const key = sub.join('★')
      // 5. 判断pathMap中有没有key
      if (key in pathMap) {
        // 6. 存在
        pathMap[key].push(item.id)
      } else {
        // 7. 不存在
        pathMap[key] = [item.id]
      }
    })
  })

  return pathMap
}

// console.log(getPathMap())

// 获取选中的规格
const getSelectedSpec = () => {
  const arr: string[] = []
  props.goods.specs.forEach((item) => {
    const temp = item.values.find((sub) => sub.selected)
    arr.push(temp ? temp.name : '')
  })
  return arr
}

const pathMap = getPathMap()
console.log(pathMap)

// 禁用按钮的方法
// 按钮时根据specs遍历生成的，因此需要给spces添加disabled属性用于控制按钮
const updateDisabledStatus = () => {
  // console.log('我执行吗')
  // const res = getSelectedSpec()
  // console.log(res)
  props.goods.specs.forEach((item, index) => {
    // 判断规格中某个值是否要被禁用
    const selectedSpec = getSelectedSpec()
    item.values.forEach((sub) => {
      // 把当前需要判断的规格放到 选中的数组中
      selectedSpec[index] = sub.name
      const key = selectedSpec.filter((v) => v).join('★')
      // console.log(key)
      if (key in pathMap) {
        sub.disabled = false
      } else {
        sub.disabled = true
      }
      // console.log(selectedSpec)
      // if (sub.name in pathMap) {
      //   // 当前规格的名字在pathMap存在，不禁用
      //   sub.disabled = false
      // } else {
      //   // 当前规格在pathMap找不到，禁用
      //   sub.disabled = true
      // }
    })
  })
}

updateDisabledStatus()
</script>
<template>
  <div class="goods-sku">
    <dl v-for="item in goods.specs" :key="item.name">
      <dt>{{ item.name }}</dt>
      <dd>
        <!-- 一个规格有多个值 -->
        <template v-for="sub in item.values" :key="sub.name">
          <img
            @click="changeSelected(item, sub)"
            v-if="sub.picture"
            :src="sub.picture"
            alt=""
            :title="sub.name"
            :class="{ selected: sub.selected, disabled: sub.disabled }"
          />
          <span
            :class="{ selected: sub.selected, disabled: sub.disabled }"
            @click="changeSelected(item, sub)"
            v-else
            >{{ sub.name }}</span
          >
        </template>
      </dd>
    </dl>
    <!-- <dl>
      <dt>尺寸</dt>
      <dd>
        <span class="disabled">10英寸</span>
        <span class="selected">20英寸</span>
        <span>30英寸</span>
      </dd>
    </dl>
    <dl>
      <dt>版本</dt>
      <dd>
        <span>美版</span>
        <span>港版</span>
      </dd>
    </dl> -->
  </div>
</template>

<style scoped lang="less">
.sku-state-mixin () {
  border: 1px solid #e4e4e4;
  margin-right: 10px;
  cursor: pointer;
  &.selected {
    border-color: @xtxColor;
  }
  &.disabled {
    opacity: 0.6;
    border-style: dashed;
    cursor: not-allowed;
  }
}
.goods-sku {
  padding-left: 10px;
  padding-top: 20px;
  dl {
    display: flex;
    padding-bottom: 20px;
    align-items: center;
    dt {
      width: 50px;
      color: #999;
    }
    dd {
      flex: 1;
      color: #666;
      > img {
        width: 50px;
        height: 50px;
        margin-top: 5px;
        .sku-state-mixin ();
      }
      > span {
        display: inline-block;
        height: 30px;
        line-height: 30px;
        padding: 0 20px;
        margin-top: 5px;
        .sku-state-mixin ();
      }
    }
  }
}
</style>
